home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / ImageIcon.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  9.7 KB  |  339 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ImageIcon.java    1.33 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.*;
  17. import java.awt.image.*;
  18. import java.net.URL;
  19.  
  20. import java.io.Serializable;
  21. import java.io.ObjectOutputStream;
  22. import java.io.ObjectInputStream;
  23. import java.io.IOException;
  24.  
  25.  
  26. /**
  27.  * An implementation of the Icon interface that paints Icons
  28.  * from Images. Images that are created from a URL or filename
  29.  * are preloaded using MediaTracker to monitor the loaded state
  30.  * of the image.
  31.  * <p>
  32.  * <strong>Warning:</strong>
  33.  * Serialized objects of this class will not be compatible with 
  34.  * future Swing releases.  The current serialization support is appropriate
  35.  * for short term storage or RMI between applications running the same
  36.  * version of Swing.  A future release of Swing will provide support for
  37.  * long term persistence.
  38.  * 
  39.  * @version 1.33 08/28/98
  40.  * @author Jeff Dinkins
  41.  */
  42. public class ImageIcon implements Icon, Serializable
  43. {
  44.     transient Image image;
  45.     transient int loadStatus = 0;
  46.     ImageObserver imageObserver;
  47.     String description = null;
  48.  
  49.     protected final static Component component = new Component() {};
  50.     protected final static MediaTracker tracker = new MediaTracker(component);
  51.  
  52.     int width = -1;
  53.     int height = -1;
  54.  
  55.     /**
  56.      * Creates an ImageIcon from the specified file. The image will
  57.      * be preloaded by using MediaTracker to monitor the loading state
  58.      * of the image.
  59.      * @param filename the name of the file containing the image
  60.      * @param description a brief textual description of the image
  61.      * @see #ImageIcon(String)
  62.      */
  63.     public ImageIcon(String filename, String description) {
  64.     image = Toolkit.getDefaultToolkit().getImage(filename);
  65.         this.description = description;
  66.     loadImage(image);
  67.     }
  68.  
  69.     /**
  70.      * Creates an ImageIcon from the specified file. The image will
  71.      * be preloaded by using MediaTracker to monitor the loading state
  72.      * of the image. The specified String can be a file name or a
  73.      * file path. When specifying a path, use the Internet-standard
  74.      * forward-slash ("/") as a separator. For example, specify:<pre>
  75.      *    new ImageIcon("images/myImage.gif")
  76.      * </pre>
  77.      * (The string is converted to an URL, so the forward-slash works
  78.      * on all systems.)
  79.      *
  80.      * @param filename a String specifying a filename or path
  81.      */
  82.     public ImageIcon (String filename) {
  83.         this(filename, filename);
  84.     }
  85.  
  86.     /**
  87.      * Creates an ImageIcon from the specified URL. The image will
  88.      * be preloaded by using MediaTracker to monitor the loaded state
  89.      * of the image.
  90.      * @param URL the URL for the image
  91.      * @param description a brief textual description of the image
  92.      * @see #ImageIcon(String)
  93.      */
  94.     public ImageIcon(URL location, String description) {
  95.     image = Toolkit.getDefaultToolkit().getImage(location);
  96.         this.description = description;
  97.     loadImage(image);
  98.     }
  99.  
  100.     /**
  101.      * Creates an ImageIcon from the specified URL. The image will
  102.      * be preloaded by using MediaTracker to monitor the loaded state
  103.      * of the image.
  104.      */
  105.     public ImageIcon (URL location) {
  106.         this(location, location.toExternalForm());
  107.     }
  108.  
  109.     /**
  110.      * Creates an ImageIcon from the image. 
  111.      * @param image the image
  112.      * @param description a brief textual description of the image
  113.      */
  114.     public ImageIcon(Image image, String description) {
  115.         this(image);
  116.         this.description = description;
  117.     }
  118.  
  119.     /**
  120.      * Creates an ImageIcon from an image object. 
  121.      */
  122.     public ImageIcon (Image image) {
  123.     this.image = image;
  124.         Object o = image.getProperty("comment", imageObserver);
  125.         if (o instanceof String) {
  126.             description = (String) o;
  127.         }
  128.     loadImage(image);
  129.     }
  130.  
  131.     /**
  132.      * Creates an ImageIcon from an array of bytes which were
  133.      * read from an image file containing a supported image format,
  134.      * such as GIF or JPEG.  Normally this array is created
  135.      * by reading an image using Class.getResourceAsStream(), but
  136.      * the byte array may also be statically stored in a class.
  137.      *
  138.      * @param  imageData an array of pixels in an image format supported
  139.      *         by the AWT Toolkit, such as GIF or JPEG.
  140.      * @param  description a brief textual description of the image
  141.      * @see    java.awt.Toolkit#createImage
  142.      */
  143.     public ImageIcon (byte[] imageData, String description) {
  144.     this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  145.         if (image == null) {
  146.             return;
  147.         }
  148.         this.description = description;
  149.     loadImage(image);
  150.     }
  151.  
  152.     /**
  153.      * Creates an ImageIcon from an array of bytes which were
  154.      * read from an image file containing a supported image format,
  155.      * such as GIF or JPEG.  Normally this array is created
  156.      * by reading an image using Class.getResourceAsStream(), but
  157.      * the byte array may also be statically stored in a class.
  158.      *
  159.      * @param  an array of pixels in an image format supported by
  160.      *         the AWT Toolkit, such as GIF or JPEG.
  161.      * @see    java.awt.Toolkit#createImage
  162.      */
  163.     public ImageIcon (byte[] imageData) {
  164.     this.image = Toolkit.getDefaultToolkit().createImage(imageData);
  165.         if (image == null) {
  166.             return;
  167.         }
  168.         Object o = image.getProperty("comment", imageObserver);
  169.         if (o instanceof String) {
  170.             description = (String) o;
  171.         }
  172.     loadImage(image);
  173.     }
  174.  
  175.     /**
  176.      * Creates an uninitialized image icon.
  177.      */
  178.     public ImageIcon() {
  179.     }
  180.  
  181.     /**
  182.      * Wait for the image to load
  183.      */
  184.     protected void loadImage(Image image) {
  185.     synchronized(tracker) {
  186.         tracker.addImage(image, 0);
  187.         try {
  188.         tracker.waitForID(0, 5000);
  189.         } catch (InterruptedException e) {
  190.         System.out.println("INTERRUPTED while loading Image");
  191.         }
  192.             loadStatus = tracker.statusID(0, false);
  193.         tracker.removeImage(image, 0);
  194.  
  195.         width = image.getWidth(imageObserver);
  196.         height = image.getHeight(imageObserver);
  197.     }
  198.     }
  199.  
  200.     /**
  201.      * Returns the status of the image loading operation.
  202.      * @return the loading status as defined by java.awt.MediaTracker.
  203.      * @see java.awt.MediaTracker#ABORTED
  204.      * @see java.awt.MediaTracker#ERRORED
  205.      * @see java.awt.MediaTracker#COMPLETE
  206.      */
  207.     public int getImageLoadStatus() {
  208.         return loadStatus;
  209.     }
  210.  
  211.     /**
  212.      * Returns the Icon's Image
  213.      */
  214.     public Image getImage() {
  215.     return image;
  216.     }
  217.  
  218.     /**
  219.      * Set the image displayed by this icon.
  220.      */
  221.     public void setImage(Image image) {
  222.     this.image = image;
  223.     loadImage(image);
  224.     }
  225.  
  226.     /**
  227.      * Get the description of the image.  This is meant to be a brief
  228.      * textual description of the object.  For example, it might be
  229.      * presented to a blind user to give an indication of the purpose
  230.      * of the image.
  231.      */
  232.     public String getDescription() {
  233.     return description;
  234.     }
  235.  
  236.     /**
  237.      * Set the description of the image.  This is meant to be a brief
  238.      * textual description of the object.  For example, it might be
  239.      * presented to a blind user to give an indication of the purpose
  240.      * of the image.
  241.      */
  242.     public void setDescription(String description) {
  243.     this.description = description;
  244.     }
  245.  
  246.     /**
  247.      * Paints the Icon
  248.      */
  249.     public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
  250.         if(imageObserver == null) {
  251.            g.drawImage(image, x, y, c);
  252.         } else {
  253.        g.drawImage(image, x, y, imageObserver);
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * Get the width of the Icon
  259.      */
  260.     public int getIconWidth() {
  261.     return width;
  262.     }
  263.  
  264.     /**
  265.      * Get the height of the Icon
  266.      */
  267.     public int getIconHeight() {
  268.     return height;
  269.     }
  270.  
  271.     /** 
  272.      * Set the image observer for the image.  Set this
  273.      * property if the ImageIcon contains an animated GIF, so
  274.      * the observer is notified to update its display.
  275.      * For example:
  276.      * <pre>
  277.      *     icon = new ImageIcon(...)
  278.      *     button.setIcon(icon);
  279.      *     icon.setImageObserver(button);
  280.      * </pre>
  281.      */
  282.     public void setImageObserver(ImageObserver observer) {
  283.         imageObserver = observer;
  284.     }
  285.  
  286.     /**
  287.      *  Return the umage observer for the image 
  288.      */
  289.     public ImageObserver getImageObserver() {
  290.         return imageObserver;
  291.     }
  292.  
  293.  
  294.     private void readObject(ObjectInputStream s)
  295.     throws ClassNotFoundException, IOException 
  296.     {
  297.     s.defaultReadObject();
  298.     
  299.     int w = s.readInt();
  300.     int h = s.readInt();
  301.     int[] pixels = (int[])(s.readObject());
  302.  
  303.         if (pixels != null) {
  304.         Toolkit tk = Toolkit.getDefaultToolkit();
  305.         ColorModel cm = ColorModel.getRGBdefault();
  306.         image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
  307.         } 
  308.     }
  309.  
  310.  
  311.     private void writeObject(ObjectOutputStream s) 
  312.     throws IOException 
  313.     {
  314.     s.defaultWriteObject();
  315.  
  316.     int w = getIconWidth();
  317.     int h = getIconHeight();
  318.     int[] pixels = image != null? new int[w * h] : null;
  319.  
  320.         if (image != null) {
  321.         try {
  322.             PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
  323.             pg.grabPixels();
  324.             if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
  325.             throw new IOException("failed to load image contents");
  326.             }
  327.         }
  328.         catch (InterruptedException e) {
  329.             throw new IOException("image load interrupted");
  330.         }
  331.         }
  332.     
  333.     s.writeInt(w);
  334.     s.writeInt(h);
  335.     s.writeObject(pixels);
  336.     }
  337. }
  338.  
  339.